package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.service.SetMealService;
import com.itheima.utils.DateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.*;

/**
 * 报表管理
 */
@RestController
@RequestMapping("/report")
public class ReportController {
    @Reference
    private MemberService memberService;

    @Reference
    private SetMealService setmealService;





//    @RequestMapping("/getSetmealReportMoney")
//    public Result getSetmealReportMoney(){
//        return setmealService.getSetmealReportMoney();
//    }
//一周内体检收入统计
@RequestMapping("getSetmealReportMoney")
public Result getSetmealReportMoney(){
    try {
        //使用工具类找到当前时间前一周内每天的时间日期
        ArrayList<String> dayList = DateUtils.findDaysToWeek();

        List<String> names = setmealService.findSetmealNameCount();   //套餐名称组

        Map<String, Object> date = setmealMoneyToDate(names, dayList);


        return new Result(true, "体检收入统计查询成功", date);
    }catch (Exception e){
        e.printStackTrace();
        return new Result(true, "体检收入统计查询失败");
    }
}




    @RequestMapping("/getSetmealReportMoneyDate")
    public Result getSetmealReportMoneyDate(@RequestParam List<String> value1){
        try {
            String start = value1.get(0);
            String end = value1.get(1);

            Date sd = DateUtils.parseString2Date(start);
            Date ed = DateUtils.parseString2Date(end);
            Date today = new Date();
            if (sd.getTime()>today.getTime() || ed.getTime()>today.getTime()){
                return new Result(false,"未来不可预测哦");
            }

            List<Map> list = setmealService.findTaoCanName(start,end);

            Integer money = setmealService.findZongJinMoney(start,end);
            Map<String,Object> map = new HashMap<>();
            map.put("name","总收入");
            map.put("value",money);
            list.add(map);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }




    /**
     * 获取套餐在区域时间内的金额
     * @param names 所有套餐名
     * @param dayList 区域时间
     * @return 对应套餐在区域时间内的总金额
     */
    public Map<String, Object>  setmealMoneyToDate(List<String> names,  ArrayList<String> dayList){
        Map<String, Object> date = new HashMap();

        List<Map<String, Object>> moneys = new ArrayList<>();

        double sum = 0.0;
        //找到每个套餐名
        for (String name : names) {
            //找到每个套餐的id
            Integer id = setmealService.findnameById(name);

            //当前套餐的金额
            Integer money = setmealService.findSetmealMoney(id);


            //查到当前套餐在时间内的数量
            Double num = setmealService.findSetmealNum(id, dayList);

            Map<String, Object> map = new HashMap<>();

            //当前套餐在时间内的金额
            double znum = money * num;

            sum+=znum;

            map.put("name", name);
            map.put("value", znum);
            moneys.add(map);
        }

        Map<String, Object> map = new HashMap<>();


        //最后将总收入放到集合中
        names.add("总收入");
        map.put("name","总收入");
        map.put("value", sum);
        moneys.add(map);

        date.put("moneys", moneys);
        date.put("names", names);
        return date;
    }




}
